<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="inputValue" @change="inputText"/>
    <a-button type="primary" @click="addThing">添加事项</a-button>

    <a-list bordered :dataSource="showClickData" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="(e)=>{finshThings(e,item.id)}">{{ item.info }}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="deleteThing(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{getRemainNum}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="viewKey==='all'? 'primary':''" @click="clickChange('all')">全部</a-button>
          <a-button :type="viewKey==='notFinsh'? 'primary':''" @click="clickChange('notFinsh')">未完成</a-button>
          <a-button :type="viewKey==='finsh'? 'primary':''" @click="clickChange('finsh')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="removeFinsh">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import {mapState,mapGetters} from 'vuex'
export default {
  created(){
    // 调用store中的actions中的函数需要用dispatch,调用mutations中的函数使用commit
    this.$store.dispatch('getDataList')
  },
  computed:{
    ...mapState(['dataList','inputValue','viewKey']),
    ...mapGetters(['getRemainNum','showClickData'])
  },
  methods:{
    // e.target.value是输入框的内容
    inputText(e){
      this.$store.commit('changeInput',e.target.value)
    },
    addThing(){
      if(this.inputValue.trim().length===0){
        return this.$message.warning('输入内容不能为空')
      }
      this.$store.commit('addList')

    },
    deleteThing(thingId){
      this.$store.commit('deleteThing',thingId)
    },
    finshThings(e,thingId){
      const params={
        id:thingId,
        checkStatus:e.target.checked,
      }
      this.$store.commit('finshThings',params)
    },
    removeFinsh(){
      this.$store.commit('removeFinsh')
    },
    clickChange(viewName){
      this.$store.commit('clickChange',viewName)
    },
  }
}
</script>
<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>